package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.Dto.DishDto;
import com.itheima.Emtity.Dish;
import com.itheima.Emtity.DishFlavor;
import com.itheima.mapper.DishMapper;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;


    /**
     * 新增菜品和新增口味数据
     * 因为口味要对应着菜品ID
     */
    @Override
    @Transactional
    public void SaveDishFlavor(DishDto dishDto) {
//        保存菜品的基本信息到菜品表dish
//        dishDto里面有菜品和口味.
        this.save(dishDto);
//        菜品id
        Long id = dishDto.getId();
//        菜品口味处理
//        菜品口味集合
        List<DishFlavor> flavors = dishDto.getFlavors();

//item一个个菜品口味.
//也可以用set
        flavors = flavors.stream().map((item) -> {
//            添加菜品的id.对应口味表.
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
//        保存菜品口味到菜品口味表dish_favor
    }


    public DishDto GetByIdWithFlavors(Long id) {
        //根据id找到菜品数据.
        Dish dish = this.getById(id);
        System.out.println(dish);
//        菜品信息但是缺少口味信息.需要根据
        //之后根据id找到菜品口味表.
        //因为根据菜品的id找到菜品口味的id.
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(lambdaQueryWrapper);
        System.out.println(flavors);
        // Dish  +  DishFlavors  =  DishDto
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    @Override
    @Transactional
    public void UpdateDishFlavor(DishDto dishDto) {

        // 更新dish表的基本信息.update (相对简单,)
        log.info(String.valueOf(dishDto));

//        this.updateById();括号里面是Dish的,为什么可以放dishDto呢?因为这是多态,Dish  dish=DIshDto;是可以这样的.
//        到时候更新就是拿dish里面的字段,dishdto多余的就没有拿.
        this.updateById(dishDto);

//        清理当前菜品对应的菜品口味,delete

        //  根据菜品的id删除口味.
//delete from Dishflavor where dish_id=?;
        //先构建where 之后运行.
        Long id = dishDto.getId();
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, id);

        dishFlavorService.remove(lambdaQueryWrapper);

        ///增加口味信息,insert
//        口味表数据
        List<DishFlavor> flavors = dishDto.getFlavors();


//        flavors = flavors.stream().map((item) -> {
////            添加菜品的id.对应口味表.
//            item.setDishId(dishDto.getId());
//            return item;
//        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }


}
